﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdministradorForm
{
    public partial class SeleccionarTecnicoForm : PlantillaForm
    {
        private SeleccionarPedidoForm formPadre;
        private int nroEmp = 0;
        private int id_pedido;
        ServiceReferenceAdmin.Service1SoapClient srv = new AdministradorForm.ServiceReferenceAdmin.Service1SoapClient();

        public SeleccionarTecnicoForm(SeleccionarPedidoForm form)
        {
            InitializeComponent();
            formPadre = form;
            textBox1.Text = form.Id_pedido.ToString();
            id_pedido = form.Id_pedido;
            dataGridView1.DataSource = srv.ListarTecnicosMenosOcupados();
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            Close();
        }

        /*private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            nroEmp = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
        }*/

        private void button1_Click(object sender, EventArgs e)
        {
            if (nroEmp == 0)
                MessageBox.Show("Debe seleccionar un técnico para realizar la consulta", "Error en selección de técnico", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                IAsyncResult res = srv.BeginAsignarPedido(id_pedido, nroEmp, new AsyncCallback(ResultButton1), srv);
            }
        }

        private void ResultButton1(IAsyncResult res)
        {
            this.Invoke(new AsyncCallback(ActualizarPantalla), new object[] { res });
        }

        private void ActualizarPantalla(IAsyncResult res)
        {
            ServiceReferenceAdmin.Service1SoapClient srv = (ServiceReferenceAdmin.Service1SoapClient)res.AsyncState;
            srv.EndAsignarPedido(res);
            //formPadre.Close();
            
            Close();
            
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(textBox1, "Seleccione un pedido");
            }
            else
                errorProvider1.SetError(textBox1, "");
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            nroEmp = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
        }
    }
}
